import 'ace-builds/src-noconflict/mode-sql';

// 应用自定义折叠规则
const Range = ace.require("ace/range").Range;
const BaseFoldMode = ace.require("ace/mode/folding/fold_mode").FoldMode;

// 自定义 SQL 折叠规则
const SqlFoldMode = function() {
    this.foldingStartMarkers = {
        'SELECT': /\bSELECT\b/i,
        'CASE': /\bCASE\b/i,
        '(': /\(/,
    };
    this.foldingStopMarkers = {
        'END': /\bEND\b/i,
        'FROM': /\bFROM\b/i,
        ')': /\)/,
    };
};

SqlFoldMode.prototype = Object.create(BaseFoldMode.prototype);
SqlFoldMode.prototype.constructor = SqlFoldMode;

SqlFoldMode.prototype.getFoldWidget = function(session, foldStyle, row) {
    const line = session.getLine(row);
    for (let marker in this.foldingStartMarkers) {
        if (this.foldingStartMarkers[marker].test(line)) {
            return "start";
        }
    }
    return "";
};

SqlFoldMode.prototype.getFoldWidgetRange = function(session, foldStyle, row) {
    const line = session.getLine(row);
    for (let marker in this.foldingStartMarkers) {
        const match = line.match(this.foldingStartMarkers[marker]);
        if (match) {
            const startColumn = match.index + match[0].length;
            const startRow = row;

            const maxRow = session.getLength();
            let endRow = row;
            let endColumn = line.length;

            let openBrackets = 0;
            let openCases = 0;
            let inString = false;
            let inComment = false;

            while (++row < maxRow) {
                const line = session.getLine(row);

                // 切换字符串和注释状态
                for (let i = 0; i < line.length; i++) {
                    if (line[i] === "'" || line[i] === "\"") {
                        inString = !inString;
                    }
                    if (line.substr(i, 2) === "/*") {
                        inComment = true;
                    }
                    if (line.substr(i, 2) === "*/") {
                        inComment = false;
                    }
                }

                // 忽略字符串和注释中的内容
                if (inString || inComment) continue;

                if (line.match(this.foldingStartMarkers['('])) {
                    openBrackets++;
                } else if (line.match(this.foldingStartMarkers['CASE'])) {
                    openCases++;
                }

                // 处理结束标记
                if (marker === 'SELECT') {
                    const fromMatch = line.match(this.foldingStopMarkers['FROM']);
                    if (fromMatch) {
                        endRow = row;
                        endColumn = fromMatch.index; // 保留 FROM 关键字
                        break;
                    }
                } else if (marker === 'CASE') {
                    const endMatch = line.match(this.foldingStopMarkers['END']);
                    if (endMatch) {
                        if (openCases === 0) {
                            endRow = row;
                            endColumn = endMatch.index; // 保留 END 关键字
                            break;
                        } else {
                            openCases--;
                        }
                    }
                } else if (marker === '(') {
                    const closeBracketMatch = line.indexOf(')');
                    if (closeBracketMatch !== -1 && openBrackets === 0) {
                        endRow = row;
                        endColumn = closeBracketMatch; // 保留右括号
                        break;
                    } else if (closeBracketMatch !== -1) {
                        openBrackets--;
                    }
                }
            }

            if (endRow > startRow) {
                return new Range(startRow, startColumn, endRow, endColumn);
            }
        }
    }
};

export default SqlFoldMode;
